package com.eco.fanliapp.service.impl;

import com.eco.fanliapp.entity.TaobaoScheme;
import com.eco.fanliapp.service.IJobOrderInfoHandleNewService;
import com.eco.fanliapp.service.ITaoBaoOrderNewService;
import com.eco.fanliapp.utils.DateCalculateUtils;
import com.eco.fanliapp.utils.DateUtils;
import com.eco.fanliapp.vo.job.CountLauncherDone;
import com.eco.fanliapp.vo.job.CountLauncherFail;
import com.eco.fanliapp.vo.job.CountLauncherPay;
import com.eco.fanliapp.vo.job.CountLauncherSuccess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class JobOrderInfoHandleNewServiceImpl implements IJobOrderInfoHandleNewService {

    private static final Logger log = LoggerFactory.getLogger(JobOrderInfoHandleNewServiceImpl.class);


    @Autowired
    private ITaoBaoOrderNewService taoBaoOrderNewService;

    /**
     * 付款订单处理方法
     *
     * @param startTime
     * @param endTime
     * @param taobaoSchemes
     */
    @Override
    public void handlePayOrder(String startTime, String endTime, List<TaobaoScheme> taobaoSchemes, int type) {

        //当前时间
        long startMillions = System.currentTimeMillis();

        //淘客订单状态，12-付款，13-关闭，14-确认收货，3-结算成功;不传，表示所有状态
        Long tkStatus = 12L;
        //member_type   推广者角色类型,2:二方，3:三方，不传，表示所有角色
        Long orderCountType = 2L;
        //查询时间类型，1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询
        String orderQueryType = "2";
        //String orderQueryType = "create_time";
        //Long orderScene = 2L;//场景订单场景类型，1:常规订单，2:渠道订单，3:会员运营订单，默认为1

        try {

            if (taobaoSchemes.size() > 0) {
//                //member_type 推广者角色类型,2:二方，3:三方
//                for (long member_type = startIndex; member_type <= endIndex; member_type++) {
                    //orderScene 订单类型2:渠道订单，3:会员运营订单
                    for (long orderScene = 2; orderScene <= 3; orderScene++) {

                        CountLauncherPay countLauncherPay  = new CountLauncherPay(taobaoSchemes.size());
                        taoBaoOrderNewService.setCountLauncherPay(countLauncherPay);

                        Long pageNo = 1L;
                        for (TaobaoScheme taobaoScheme2 : taobaoSchemes) {
                            taoBaoOrderNewService.getPayOrderDetail(
                                    taobaoScheme2.getAppSession(), startTime, endTime, pageNo, tkStatus,
                                    0L, orderQueryType, orderScene, type,null);
                        }

                        try {
                            Thread.sleep(500 * (type + 1));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        while (countLauncherPay.getCount() > 0) {
                            if ((System.currentTimeMillis() - startMillions) > 1000 * 60 * 2) {
                                break;
                            }
                            log.info("*********等待异步方法执行完毕-付款订单定时任务" + "--countLauncherPay=" + countLauncherPay.getCount() + "--time=" + System.currentTimeMillis());
                            try {
                                Thread.sleep(2000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    }
//                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("*********一次拉取付款订单错误" + "--startTime=" + startTime + "--endTime=" + endTime);
        } finally {
            log.info("*********结束一次拉取付款订单" + "--startTime=" + startTime + "--endTime=" + endTime);
        }
    }

    /**
     * 失效订单处理方法
     * @param startTime
     * @param endTime
     * @param taobaoSchemes
     */
    @Override
    public void handleFailOrder(String startTime, String endTime, List<TaobaoScheme> taobaoSchemes, int type) {

        //当前时间
        long startMillions = System.currentTimeMillis();
        //淘客订单状态，12-付款，13-关闭，14-确认收货，3-结算成功;不传，表示所有状态
        Long tkStatus = 13L;
        //member_type   推广者角色类型,2:二方，3:三方，不传，表示所有角色
        Long orderCountType = 2L;
        //String orderQueryType = "create_time";
        //查询时间类型，1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询
        String orderQueryType = "1";
        //场景订单场景类型，1:常规订单，2:渠道订单，3:会员运营订单，默认为1
        //Long orderScene = 2L;

        try {
            if (taobaoSchemes.size() > 0) {

//                for (long j = 2; j <= 3; j++) {
                    for (long orderScene = 2; orderScene <= 3; orderScene++) {

                        CountLauncherFail countLauncherFail = new CountLauncherFail(taobaoSchemes.size());
                        taoBaoOrderNewService.setCountLauncherFail(countLauncherFail);

                        Long pageNo = 1L;
                        for (TaobaoScheme taobaoScheme2 : taobaoSchemes) {
                            taoBaoOrderNewService.getFailOrderDetail(
                                    taobaoScheme2.getAppSession(), startTime, endTime, pageNo, tkStatus,
                                    0L, orderQueryType,orderScene, type,null);
                            try {
                                Thread.sleep(200);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        while (countLauncherFail.getCount() > 0) {
                            if ((System.currentTimeMillis() - startMillions) > 1000 * 60 * 2) {
                                break;
                            }
                            log.info("*********等待异步方法执行完毕-一次拉取失效订单" + "--countLauncherFail=" + countLauncherFail.getCount() + "--time=" + System.currentTimeMillis());
                            try {
                                Thread.sleep(2000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("*********一次拉取失效订单错误" + "--startTime=" + startTime + "--endTime=" + endTime);
        }finally {
            log.info("*********结束一次拉取失效订单" + "--startTime=" + startTime + "--endTime=" + endTime);
        }

    }

    /**
     *完成订单处理方法
     * @param startTime
     * @param endTime
     * @param taobaoSchemes
     */
    @Override
    public void handleCompleteOrder(String startTime, String endTime, List<TaobaoScheme> taobaoSchemes, int type) {

        //当前时间
        long startMillions = System.currentTimeMillis();
        //淘客订单状态，12-付款，13-关闭，14-确认收货，3-结算成功;不传，表示所有状态
        Long tkStatus = 14L;
        //member_type   推广者角色类型,2:二方，3:三方，不传，表示所有角色
        Long orderCountType = 2L;
//        String orderQueryType = "create_time";
        //查询时间类型，1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询
        String orderQueryType = "1";
        //场景订单场景类型，1:常规订单，2:渠道订单，3:会员运营订单，默认为1
//        Long orderScene = 2L;

        try {
            if(taobaoSchemes.size() > 0) {

//                for (long j = 2; j <= 3; j++) {
                    for (long orderScene = 2; orderScene <= 3; orderScene++) {

                        CountLauncherDone countLauncherDone = new CountLauncherDone(taobaoSchemes.size());
                        taoBaoOrderNewService.setCountLauncherDone(countLauncherDone);

                        Long pageNo = 1L;
                        for (TaobaoScheme taobaoScheme2 : taobaoSchemes) {
                            taoBaoOrderNewService.getSuccOrderDetail(taobaoScheme2.getAppSession(), startTime, endTime, pageNo,
                                            tkStatus, 0L, orderQueryType, orderScene, type, null);
                            try {
                                Thread.sleep(200);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        while (countLauncherDone.getCount() > 0) {
                            if ((System.currentTimeMillis() - startMillions) > 1000 * 60 * 2) {
                                break;
                            }
                            log.info("*********等待异步方法执行完毕-一次拉取完成订单" + "--countLauncherDone=" + countLauncherDone.getCount() + "--time=" + System.currentTimeMillis());
                            try {
                                Thread.sleep(2000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("*********一次拉取完成订单错误" + "--startTime=" + startTime + "--endTime=" + endTime);
        }finally {
            log.info("*********结束一次拉取完成订单" + "--startTime=" + startTime + "--endTime=" + endTime);
        }

    }

    /**
     *结算订单处理方法
     * @param startTime
     * @param endTime
     * @param taobaoSchemes
     * @param type 0  20分钟内定时任务 1  规定时间内定时任务
     */
    @Override
    public void handleSettleOrder(String startTime, String endTime, List<TaobaoScheme> taobaoSchemes,int type) {

        //当前时间
        long startMillions = System.currentTimeMillis();

        //淘客订单状态，12-付款，13-关闭，14-确认收货，3-结算成功;不传，表示所有状态
        Long tkStatus = 3L;
        //member_type   推广者角色类型,2:二方，3:三方，不传，表示所有角色
        Long orderCountType = 2L;
//            String orderQueryType = "settle_time";
        //查询时间类型，1：按照订单淘客创建时间查询，2:按照订单淘客付款时间查询，3:按照订单淘客结算时间查询
        String orderQueryType = "3";
        //场景订单场景类型，1:常规订单，2:渠道订单，3:会员运营订单，默认为1
//            Long orderScene = 2L;
        try {

            if(taobaoSchemes.size() > 0) {

//                for (long j = 2; j <= 3; j++) {
                    for (long orderScene = 2; orderScene <= 3; orderScene++) {
                        CountLauncherSuccess countLauncherSuccess = null;

                        countLauncherSuccess = new CountLauncherSuccess(taobaoSchemes.size());
                        taoBaoOrderNewService.setCountLauncherSuccess(countLauncherSuccess);

                        Long pageNo = 1L;
                        for (TaobaoScheme taobaoScheme2 : taobaoSchemes) {
                            taoBaoOrderNewService.getSettleOrderDetail(taobaoScheme2.getAppSession(), startTime, endTime, pageNo, tkStatus,
                                    0L, orderQueryType, orderScene, type, null);
                        }
                        try {
                            Thread.sleep(500 * (type + 1));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        while (countLauncherSuccess.getCount() > 0) {
                            if ((System.currentTimeMillis() - startMillions) > 1000 * 60 * 2) {
                                break;
                            }
                            log.info("*********等待异步方法执行完毕-结算订单定时任务" + "--countLauncherSuccess=" + countLauncherSuccess.getCount() + "--time=" + System.currentTimeMillis());
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                    }
//                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("*********一次拉取结算订单错误" + "--startTime=" + startTime + "--endTime=" + endTime);
        }finally {
            log.info("*********结束一次拉取结算订单" + "--startTime=" + startTime + "--endTime=" + endTime);
        }

    }

    /**
     * 处理时间
     *
     * @param timeFlag
     * @return
     */
    @Override
    public List<String> handleTime(String timeFlag, int day) {
        //当前日期参照点
        Date currentTime = new Date();
        String currentTimeStr = DateUtils.date2Str(currentTime, null);
        if (timeFlag == null || "".equals(timeFlag)) {
            java.util.Calendar calendar = java.util.Calendar.getInstance();
            calendar.setTime(currentTime);
            calendar.add(java.util.Calendar.MINUTE, -(24 * 60 * day));

            timeFlag = DateUtils.date2Str(calendar.getTime(), null);
            log.info("开始订单时间参照点{}", timeFlag);
        }
        //切割时间段(交易类型 M/D/H/N -->每月/每天/每小时/每分钟,多久分成一份，开始时间，结束时间)
        List<String> list = DateCalculateUtils.cutDate("H", 2, timeFlag, currentTimeStr);
        return list;
    }

}
